package com.aizhixin.lab.company.controller;

import com.aizhixin.lab.account.dto.AccountDTO;
import com.aizhixin.lab.account.service.AccountService;
import com.aizhixin.lab.common.core.ApiReturnConstants;
import com.aizhixin.lab.common.utils.TokenUtil;
import com.aizhixin.lab.company.domain.OrganDomain;
import com.aizhixin.lab.company.domain.SystemSetDomain;
import com.aizhixin.lab.company.service.ClassesService;
import com.aizhixin.lab.company.service.CollegeService;
import com.aizhixin.lab.company.service.ProfessionalService;
import com.aizhixin.lab.company.service.SystemSetService;
import com.aizhixin.lab.company.service.UserService;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/api/v1/base/organ")
@Api(value = "组织架构相关API")
public class OrganizationController {
	@Autowired
	private AccountService accountService;
	@Autowired
	private CollegeService collegeService;
	@Autowired
	private ProfessionalService professionalService;
	@Autowired
	private ClassesService classesService;
	@Autowired
	private UserService userService;

	@RequestMapping(value = "/saveorgan", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE)
	@ApiOperation(httpMethod = "POST", value = "保存组织架构", response = Void.class, notes = "保存组织架构<br>@author zhengning")
	public ResponseEntity<?> saveInfor(@RequestHeader("Authorization") String token,
			@ApiParam(value = "perfessionalId和collegeId都存在为添加班级信息,仅collegeId值存在则为添加专业信息，否则为添加院系信息", required = false) @RequestBody OrganDomain dto) {
		AccountDTO account = accountService.getUserByToken(token);
		if (account == null) {
			return new ResponseEntity<Object>(TokenUtil.tokenValid(), HttpStatus.UNAUTHORIZED);
		}
		Map<String, Object> result = null;
		if (StringUtils.isEmpty(dto.getName())) {
			result = new HashMap<>();
			result.put(ApiReturnConstants.SUCCESS, false);
			result.put(ApiReturnConstants.ERROR, "名称不能为空!");
		} else {
			if (null != dto.getCollegeId() && dto.getCollegeId() > 0) {
				if (null != dto.getProfessionalId() && dto.getProfessionalId() > 0) {
					result = classesService.save(dto);
				} else {
					result = professionalService.save(dto);
				}
			} else {
				result = collegeService.save(dto);
			}
		}
		return new ResponseEntity(result, HttpStatus.OK);
	}

	@RequestMapping(value = "/getcollege", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
	@ApiOperation(httpMethod = "GET", value = "查询院系列表", response = Void.class, notes = "查询院系列表<br>@author zhengning")
	public ResponseEntity<?> getCollege() {
		Map<String, Object> result = collegeService.getAll();
		return new ResponseEntity(result, HttpStatus.OK);
	}

	@RequestMapping(value = "/getprofessional", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
	@ApiOperation(httpMethod = "GET", value = "查询专业列表", response = Void.class, notes = "查询专业列表<br>@author zhengning")
	public ResponseEntity<?> getProfessional(
			@ApiParam(value = "院系id", required = false) @RequestParam(value = "collegeId", required = false) Long collegeId) {
		Map<String, Object> result = null;
		if(null != collegeId && collegeId.longValue() > 0L) {
			result = professionalService.getAllByCollegeId(collegeId);
		}else {
			result = professionalService.getAll();
		}
		
		return new ResponseEntity(result, HttpStatus.OK);
	}
	

	@RequestMapping(value = "/delorgan", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE)
	@ApiOperation(httpMethod = "POST", value = "删除组织架构", response = Void.class, notes = "删除组织架构<br>@author zhengning")
	public ResponseEntity<?> delInfor(@RequestHeader("Authorization") String token,
			@ApiParam(value = "id必填,perfessionalId和collegeId都存在为删除班级信息,collegeId值存在则为删除专业，否则为删除院系", required = false) @RequestBody OrganDomain dto) {
		AccountDTO account = accountService.getUserByToken(token);
		if (account == null) {
			return new ResponseEntity<Object>(TokenUtil.tokenValid(), HttpStatus.UNAUTHORIZED);
		}
		Map<String, Object> result = null;
		if (null != dto.getId() && dto.getId()>0) {
			if (null != dto.getCollegeId() && dto.getCollegeId() > 0) {
				if (null != dto.getProfessionalId() && dto.getProfessionalId() > 0) {
					Long countNum = userService.countByClassId(dto.getId());
					if(null != countNum && countNum > 0){
						result = new HashMap<>();
						result.put(ApiReturnConstants.SUCCESS, false);
						result.put(ApiReturnConstants.ERROR, "该班级下有学生信息!");
						return new ResponseEntity(result, HttpStatus.OK);
					}
					result = classesService.delete(dto);
				} else {
					Long countNum = userService.countByProfessionalId(dto.getId());
					if(null != countNum && countNum > 0){
						result = new HashMap<>();
						result.put(ApiReturnConstants.SUCCESS, false);
						result.put(ApiReturnConstants.ERROR, "该专业下有学生信息!");
						return new ResponseEntity(result, HttpStatus.OK);
					}
					result = professionalService.delete(dto);
				}
			} else {
				Long countNum = userService.countByCollegeId(dto.getId());
				if(null != countNum && countNum > 0){
					result = new HashMap<>();
					result.put(ApiReturnConstants.SUCCESS, false);
					result.put(ApiReturnConstants.ERROR, "该学院下有学生或老师信息!");
					return new ResponseEntity(result, HttpStatus.OK);
				}
				result = collegeService.delete(dto);
			}
		} else if(null != dto.getClassIds() && ! dto.getClassIds().isEmpty()){
			Long countNum = userService.countByClassIdIn(dto.getClassIds());
			if(null != countNum && countNum > 0){
				result = new HashMap<>();
				result.put(ApiReturnConstants.SUCCESS, false);
				result.put(ApiReturnConstants.ERROR, "批量删除的班级列表下有学生信息!");
				return new ResponseEntity(result, HttpStatus.OK);
			}
			result = classesService.deleteByIds(dto.getClassIds());
		}else {
			result = new HashMap<>();
			result.put(ApiReturnConstants.SUCCESS, false);
			result.put(ApiReturnConstants.ERROR, "id或classIds不能为空!");
		}
		return new ResponseEntity(result, HttpStatus.OK);
	}
	
	
	@RequestMapping(value = "/getclasses", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE)
	@ApiOperation(httpMethod = "POST", value = "分页查询班级", response = Void.class, notes = "分页查询班级<br>@author zhengning")
	public ResponseEntity<?> getClasses(
			@ApiParam(value = "按班级名称模糊查询", required = false) @RequestBody OrganDomain dto) {
		Map<String, Object> result = classesService.findClassesPage(dto);
		return new ResponseEntity(result, HttpStatus.OK);
	}
	
}
